package com.example.zzl.view.fragment.SearchFragment;

import android.app.Application;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TextView;

import com.example.zzl.R;
import com.example.zzl.adapter.SearchFragment.GridViewDWAdapter;
import com.example.zzl.app.MyApplication;
import com.example.zzl.custom.widget.MyGridView;
import com.example.zzl.databinding.DrawlayoutSrLayoutBinding;
import com.example.zzl.model.bean.SearchFragment.SearchResultData;
import com.example.zzl.utils.ListViewUtils;
import com.example.zzl.view.BaseFragment;
import com.example.zzl.view.activity.SearchActivity.SearchResultActivity;

import java.util.ArrayList;

/**
 * Created by zhang on 2017/2/10.
 */

public class DrawFragment extends BaseFragment implements View.OnClickListener,AdapterView.OnItemClickListener{

    private DrawlayoutSrLayoutBinding binding;
    private ArrayList<SearchResultData.ResultBean.FilterListBean.ListHotTagBean> list;
    private GridViewDWAdapter adapter,adapter1;
    private EditText et_1,et_2;
    private MyGridView gv;
    private TextView tv_1,tv_2,tv_3,tv_4;
    private TableLayout tab;
    private String mallUrl;
    public static DrawFragment getInstance() {

        Bundle args = new Bundle();
        DrawFragment fragment = new DrawFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        list = new ArrayList<>();
        adapter = new GridViewDWAdapter(getActivity(),list);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.drawlayout_sr_layout,container,false);
        et_1 = binding.etDw1;
        et_2 = binding.etDw2;
        tab = binding.tabDw;
        tv_1 = binding.tvDwTitle;
        tv_2 = binding.tvDwPrice;
        tv_3 = binding.tvDwClean;
        tv_4 = binding.tvCenter;
        gv = binding.gvDwSr;
        MyApplication.gv = gv;
        list.clear();
        gv.setAdapter(adapter);
        gv.setFocusable(true);
        gv.setOnItemClickListener(this);
        binding.setMyClick(this);
        loadData();
        return binding.getRoot();
    }

    private void loadData(){

        SearchResultActivity.handler1 = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what == 0x222){
                    tab.setVisibility(View.VISIBLE);
                    et_1.setVisibility(View.VISIBLE);
                    et_2.setVisibility(View.VISIBLE);
                    tv_1.setVisibility(View.VISIBLE);
                    tv_2.setVisibility(View.VISIBLE);
                    tv_3.setVisibility(View.VISIBLE);
                    tv_4.setVisibility(View.VISIBLE);
                    if(SearchResultActivity.resultData.filter.size()>=3) {
//                        for (int i = 0; i < SearchResultActivity.resultData.filter.get(2).getListHotTag().size(); i++) {
//                            //list.add(SearchResultActivity.resultData.filter.get(2).getListHotTag().get(i));
//                            SearchResultActivity.resultData.filter.get(2).getListHotTag().get(i).type = 2;
//                        }
//                        adapter = new GridViewDWAdapter(getActivity(),list);
//                        gv.setAdapter(adapter);

                        list.add(null);
                        for (int i = 0; i < SearchResultActivity.resultData.filter.get(2).getListHotTag().size(); i++) {
                            list.add(SearchResultActivity.resultData.filter.get(2).getListHotTag().get(i));
                            SearchResultActivity.resultData.filter.get(2).getListHotTag().get(i).type = 1;
                        }
                        //adapter1 = new GridViewDWAdapter(getActivity(),list);
                        adapter.notifyDataSetChanged();
                        ListViewUtils.calGridViewWidthAndHeigh2(4,gv);
                        //gv.getChildAt(0).setBackgroundResource(R.drawable.dw_gv_select_shap);
                    }
                }
            }
        };
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back_left://关闭
                MyApplication.dw.closeDrawer(Gravity.RIGHT);
                break;
            case R.id.tv_sr_right://重置
                gv.setSelection(0);
                resetEdit();
                break;
            case R.id.tv_dw_clean://清除
                resetEdit();
                break;
            case R.id.tv_dw_1:
                setEdit("0","199");
                break;
            case R.id.tv_dw_2:
                setEdit("200","399");
                break;
            case R.id.tv_dw_3:
                setEdit("400","599");
                break;
            case R.id.tv_dw_4:
                setEdit("600","");
                break;
            case R.id.btn_dw_ok:
                setScreen();
                MyApplication.dw.closeDrawer(Gravity.RIGHT);
                break;
        }
    }

    private void setScreen(){
        int small,big;
        if(TextUtils.isEmpty(et_1.getText())){
            small=0;
        }else{
            small=Integer.parseInt(et_1.getText().toString());
        }
        if(TextUtils.isEmpty(et_2.getText())){
            big=0;
        }else{
            big=Integer.parseInt(et_2.getText().toString());
        }
        String url = "http://mws.biyabi.com/WebService.asmx/" +
                "GetInfoListWithBrandJson_V2?inforNation=0&infoType=0" +
                "&pageSize=20&pageIndex=1&isTop=1&isPurchasing=1&" +
                "orderBy=0&keyWord=%27%27&homeShow=0&catUrl="+
                SearchResultActivity.cate+"&mallUrl="+mallUrl+"&bigPrice="+
                big+"&smallPrice="+small;
        Message message = new Message();
        message.obj = url;
        message.what=0x000;
        SearchResultActivity.handler.sendMessage(message);

    }

    private void resetEdit() {
        et_1.setText("");
        et_2.setText("");
        et_1.setFocusable(false);
        et_2.setFocusable(false);
    }

    private void setEdit(String data1,String data2){
        et_1.setText(data1);
        et_2.setText(data2);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if(i!=0) {
            //gv.getChildAt(i).setBackgroundResource(R.drawable.dw_gv_select_shap);
            //Toast.makeText(getActivity(), list.get(i).getStrTagName(), Toast.LENGTH_SHORT).show();
            mallUrl = list.get(i).getStrTagName();
        }else{
            mallUrl="";
        }
        adapter.select(i);
        adapter.notifyDataSetChanged();
    }
}
